package com.example.administrator.yuejian_2.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.activity.PostDetailActivity;
import com.example.administrator.yuejian_2.bean.PostListBean;
import com.example.administrator.yuejian_2.viewholder.PoListVH;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/12.
 */

public class PoListAdapter extends RecyclerView.Adapter {
    private List<PostListBean> poList = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;

    public PoListAdapter(Context context,List<PostListBean> poList){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.poList.addAll(poList);

    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.item_po,parent,false);
        PoListVH holder = new PoListVH(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        PoListVH postholder = (PoListVH) holder;
        PostListBean postListBean = poList.get(position);

        String uname = postListBean.getU_name();
        postholder.username.setText(uname);

        String title = postListBean.getTitle();
        postholder.posttitle.setText(title);

        String upic = postListBean.getU_pic();
        if (!upic.equals(""))
            Picasso.with(context)
                    .load("http://10.0.2.2/YueJian16/Public/photo/"+upic)
                    .into(postholder.userpic);

        String popic = postListBean.getPic1();
        if (!popic.equals(""))
            Picasso.with(context)
                    .load("http://10.0.2.2/YueJian16/Public/photo/"+popic)
                    .into(postholder.postpic);

        final int pid = postListBean.getPic_id();
        postholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, PostDetailActivity.class);
                intent.putExtra("pid",pid);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return poList.size();
    }
}
